// Copyright (C) 1953-2022 NUDT
// Verilog module name - pulse_generation
// Version: V4.0.20221216
// Created:
//         by - fenglin
////////////////////////////////////////////////////////////////////////////
// Description:
//         packet_aggreation
///////////////////////////////////////////////////////////////////////////
`timescale 1ns/1ps

module packet_aggreation
(
	i_clk,
	i_rst_n,
	//gmii interfaces from p0
	i_gmii_rxclk_p0,
	i_gmii_dv_p0,
	iv_gmii_rxd_p0,
	i_gmii_rx_er_p0,	
	
	i_gmii_txclk_p0,
	i_gmii_en_p0,
	iv_gmii_txd_p0,
	i_gmii_tx_er_p0,
	
	//gmii interfaces from p1
	i_gmii_rxclk_p1,
	i_gmii_dv_p1,
	iv_gmii_rxd_p1,
	i_gmii_rx_er_p1,	
	
	i_gmii_txclk_p1,
	i_gmii_en_p1,
	iv_gmii_txd_p1,
	i_gmii_tx_er_p1,
	
	//gmii interfaces from p2
	i_gmii_rxclk_p2,
	i_gmii_dv_p2,
	iv_gmii_rxd_p2,
	i_gmii_rx_er_p2,	
	
	i_gmii_txclk_p2,
	i_gmii_en_p2,
	iv_gmii_txd_p2,
	i_gmii_tx_er_p2,
	
	//gmii interfaces from p3
	i_gmii_rxclk_p3,
	i_gmii_dv_p3,
	iv_gmii_rxd_p3,
	i_gmii_rx_er_p3,	
	
	i_gmii_txclk_p3,
	i_gmii_en_p3,
	iv_gmii_txd_p3,
	i_gmii_tx_er_p3,
	iv_mirror_mode,	
	iv_syn_clk,
	i_fifo_full,    
	o_fitram_rd ,	
	ov_fitram_raddr,
	iv_fitram_rdata,
	
	ov_pkt_abstract,
	o_pkt_abstract_wr
		
);

input					i_clk;
input           		i_rst_n;
	//gmii interfaces from p0
input			i_gmii_rxclk_p0;
input			i_gmii_dv_p0;
input	[7:0]	iv_gmii_rxd_p0;
input			i_gmii_rx_er_p0;	

input			i_gmii_txclk_p0;
input			i_gmii_en_p0;
input	[7:0]	iv_gmii_txd_p0;
input			i_gmii_tx_er_p0;
	
	//gmii interfaces from p1
input			i_gmii_rxclk_p1;
input			i_gmii_dv_p1;
input	[7:0]	iv_gmii_rxd_p1;
input			i_gmii_rx_er_p1;	
	
input			i_gmii_txclk_p1;
input			i_gmii_en_p1;
input	[7:0]	iv_gmii_txd_p1;
input			i_gmii_tx_er_p1;
	
	//gmii interfaces from p2
input			i_gmii_rxclk_p2;
input			i_gmii_dv_p2;
input	[7:0]	iv_gmii_rxd_p2;
input			i_gmii_rx_er_p2;	
	
input			i_gmii_txclk_p2;
input			i_gmii_en_p2;
input	[7:0]	iv_gmii_txd_p2;
input			i_gmii_tx_er_p2;
	//gmii interfaces from p3
input			i_gmii_rxclk_p3;
input			i_gmii_dv_p3;
input	[7:0]	iv_gmii_rxd_p3;
input			i_gmii_rx_er_p3;	
	
input			i_gmii_txclk_p3;
input			i_gmii_en_p3;
input	[7:0]	iv_gmii_txd_p3;
input			i_gmii_tx_er_p3;
input			[1:0]	iv_mirror_mode;
input			[63:0]	iv_syn_clk;

input					i_fifo_full;    
output			      	o_fitram_rd ;	
output			[3:0]	ov_fitram_raddr;
input			[14:0]	iv_fitram_rdata;


output			[127:0]	ov_pkt_abstract;
output			      	o_pkt_abstract_wr;
wire            w_abstract_ack_rxp0   ;
wire  [127:0]   wv_pkt_abstract_rxp0    ;
wire            w_pkt_abstract_wr_rxp0  ;
wire            w_abstract_ack_rxp1   ;
wire  [127:0]   wv_pkt_abstract_rxp1    ;
wire            w_pkt_abstract_wr_rxp1  ;
wire            w_abstract_ack_rxp2   ;
wire  [127:0]   wv_pkt_abstract_rxp2    ;
wire            w_pkt_abstract_wr_rxp2  ;
wire            w_abstract_ack_rxp3   ;
wire  [127:0]   wv_pkt_abstract_rxp3    ;
wire            w_pkt_abstract_wr_rxp3  ;
wire            w_abstract_ack_txp0   ;
wire  [127:0]   wv_pkt_abstract_txp0    ;
wire            w_pkt_abstract_wr_txp0  ;
wire            w_abstract_ack_txp1   ;
wire  [127:0]   wv_pkt_abstract_txp1    ;
wire            w_pkt_abstract_wr_txp1  ;
wire            w_abstract_ack_txp2   ;
wire  [127:0]   wv_pkt_abstract_txp2    ;
wire            w_pkt_abstract_wr_txp2  ;
wire            w_abstract_ack_txp3   ;
wire  [127:0]   wv_pkt_abstract_txp3    ;
wire            w_pkt_abstract_wr_txp3  ;

wire  [127:0]   wv_descriptor_rxp0      ;
wire            w_descriptor_wr_rxp0    ;
wire            w_descriptor_ack_rxp0   ;
wire  [127:0]   wv_descriptor_txp0      ;
wire            w_descriptor_wr_txp0    ;
wire            w_descriptor_ack_txp0   ;
wire  [127:0]   wv_descriptor_rxp1      ;
wire            w_descriptor_wr_rxp1    ;
wire            w_descriptor_ack_rxp1   ;
wire  [127:0]   wv_descriptor_txp1      ;
wire            w_descriptor_wr_txp1    ;
wire            w_descriptor_ack_txp1   ;
wire  [127:0]   wv_descriptor_rxp2      ;
wire            w_descriptor_wr_rxp2    ;
wire            w_descriptor_ack_rxp2   ;
wire  [127:0]   wv_descriptor_txp2      ;
wire            w_descriptor_wr_txp2    ;
wire            w_descriptor_ack_txp2   ;
wire  [127:0]   wv_descriptor_rxp3      ;
wire            w_descriptor_wr_rxp3    ;
wire            w_descriptor_ack_rxp3   ;
wire  [127:0]   wv_descriptor_txp3      ;
wire            w_descriptor_wr_txp3    ;
wire            w_descriptor_ack_txp3   ;
wire  [127:0]   pkt_abstract_data;
wire            rdreq  ;        
wire  [127:0]   wv_pkt_abstract   ;
wire            wv_pkt_abstract_wr;

packet_abstract_extract#(.port_type(5'h00)) packet_abstract_extract_instrx0(
.i_clk           (i_clk),
.i_rst_n         (i_rst_n),
.i_gmii_clk       (i_gmii_rxclk_p0),  
.i_gmii_en        (i_gmii_dv_p0),     
.iv_gmii_data     (iv_gmii_rxd_p0),   
.i_gmii_er        (i_gmii_rx_er_p0),  
.iv_mirror_mode   (iv_mirror_mode),            
.iv_syn_clk       (iv_syn_clk),  
.i_descriptor_ack (w_abstract_ack_rxp0 ),         
.ov_pkt_abstract  (wv_pkt_abstract_rxp0  ),            
.o_pkt_abstract_wr(w_pkt_abstract_wr_rxp0)             
);                              
packet_abstract_extract#(.port_type(5'h10)) packet_abstract_extract_insttx0(
.i_clk           (i_clk),
.i_rst_n         (i_rst_n),
.i_gmii_clk       (i_gmii_txclk_p0),  
.i_gmii_en        (i_gmii_en_p0),     
.iv_gmii_data     (iv_gmii_txd_p0),   
.i_gmii_er        (i_gmii_tx_er_p0),  
.iv_mirror_mode   (iv_mirror_mode),            
.iv_syn_clk       (iv_syn_clk), 
.i_descriptor_ack (w_abstract_ack_txp0 ),            
.ov_pkt_abstract  (wv_pkt_abstract_txp0  ),            
.o_pkt_abstract_wr(w_pkt_abstract_wr_txp0)             
);     
packet_abstract_extract#(.port_type(5'h01)) packet_abstract_extract_instrx1(
.i_clk           (i_clk),
.i_rst_n         (i_rst_n),
.i_gmii_clk       (i_gmii_rxclk_p1),  
.i_gmii_en        (i_gmii_dv_p1),     
.iv_gmii_data     (iv_gmii_rxd_p1),   
.i_gmii_er        (i_gmii_rx_er_p1),  
.iv_mirror_mode   (iv_mirror_mode),            
.iv_syn_clk       (iv_syn_clk), 
.i_descriptor_ack (w_abstract_ack_rxp1 ),            
.ov_pkt_abstract  (wv_pkt_abstract_rxp1  ),            
.o_pkt_abstract_wr(w_pkt_abstract_wr_rxp1)             
);                              
packet_abstract_extract#(.port_type(5'h11)) packet_abstract_extract_insttx1(
.i_clk           (i_clk),
.i_rst_n         (i_rst_n),
.i_gmii_clk       (i_gmii_txclk_p1),  
.i_gmii_en        (i_gmii_en_p1),     
.iv_gmii_data     (iv_gmii_txd_p1),   
.i_gmii_er        (i_gmii_tx_er_p1),  
.iv_mirror_mode   (iv_mirror_mode),            
.iv_syn_clk       (iv_syn_clk), 
.i_descriptor_ack (w_abstract_ack_txp1 ),            
.ov_pkt_abstract  (wv_pkt_abstract_txp1  ),            
.o_pkt_abstract_wr(w_pkt_abstract_wr_txp1)             
);   
packet_abstract_extract#(.port_type(5'h02)) packet_abstract_extract_instrx2(
.i_clk           (i_clk),
.i_rst_n         (i_rst_n),
.i_gmii_clk       (i_gmii_rxclk_p2),  
.i_gmii_en        (i_gmii_dv_p2),     
.iv_gmii_data     (iv_gmii_rxd_p2),   
.i_gmii_er        (i_gmii_rx_er_p2),  
.iv_mirror_mode   (iv_mirror_mode),   						        
.iv_syn_clk       (iv_syn_clk),       						
.i_descriptor_ack (w_abstract_ack_rxp2 ),                 
.ov_pkt_abstract  (wv_pkt_abstract_rxp2  ),            
.o_pkt_abstract_wr(w_pkt_abstract_wr_rxp2)             
);                              
packet_abstract_extract#(.port_type(5'h12)) packet_abstract_extract_insttx2(
.i_clk           (i_clk),
.i_rst_n         (i_rst_n),
.i_gmii_clk       (i_gmii_txclk_p2),  
.i_gmii_en        (i_gmii_en_p2),     
.iv_gmii_data     (iv_gmii_txd_p2),   
.i_gmii_er        (i_gmii_tx_er_p2),  
.iv_mirror_mode   (iv_mirror_mode),            
.iv_syn_clk       (iv_syn_clk), 
.i_descriptor_ack (w_abstract_ack_txp2 ),            
.ov_pkt_abstract  (wv_pkt_abstract_txp2  ),            
.o_pkt_abstract_wr(w_pkt_abstract_wr_txp2)             
);   
packet_abstract_extract#(.port_type(5'h03)) packet_abstract_extract_instrx3(
.i_clk           (i_clk),
.i_rst_n         (i_rst_n),
.i_gmii_clk       (i_gmii_rxclk_p3),  
.i_gmii_en        (i_gmii_dv_p3),     
.iv_gmii_data     (iv_gmii_rxd_p3),   
.i_gmii_er        (i_gmii_rx_er_p3),  
.iv_mirror_mode   (iv_mirror_mode),            
.iv_syn_clk       (iv_syn_clk),  
.i_descriptor_ack (w_abstract_ack_rxp3  ),           
.ov_pkt_abstract  (wv_pkt_abstract_rxp3  ),            
.o_pkt_abstract_wr(w_pkt_abstract_wr_rxp3)             
);                              
packet_abstract_extract#(.port_type(5'h13)) packet_abstract_extract_insttx3(
.i_clk           (i_clk),
.i_rst_n         (i_rst_n),
.i_gmii_clk       (i_gmii_txclk_p3),  
.i_gmii_en        (i_gmii_en_p3),     
.iv_gmii_data     (iv_gmii_txd_p3),   
.i_gmii_er        (i_gmii_tx_er_p3),  
.iv_mirror_mode   (iv_mirror_mode),            
.iv_syn_clk       (iv_syn_clk), 
.i_descriptor_ack (w_abstract_ack_txp3 ),            
.ov_pkt_abstract  (wv_pkt_abstract_txp3  ),            
.o_pkt_abstract_wr(w_pkt_abstract_wr_txp3)             
);   
descriptor_delay_manage #(.delay_cycle(4'd7)) descriptor_delay_manage_p00_inst
(                              
.i_clk                          (i_clk),
.i_rst_n                        (i_rst_n),
                                
.iv_descriptor                  (wv_pkt_abstract_rxp0  ),
.i_descriptor_wr                (w_pkt_abstract_wr_rxp0),
.o_descriptor_ack               (w_abstract_ack_rxp0 ),
                                  
.ov_descriptor                  (wv_descriptor_rxp0   ),
.o_descriptor_wr                (w_descriptor_wr_rxp0 ),
.i_descriptor_ack               (w_descriptor_ack_rxp0)
);
descriptor_delay_manage #(.delay_cycle(4'd7)) descriptor_delay_manage_p01_inst
(                              
.i_clk                          (i_clk),
.i_rst_n                        (i_rst_n),
                                
.iv_descriptor                  (wv_pkt_abstract_txp0  ),
.i_descriptor_wr                (w_pkt_abstract_wr_txp0),
.o_descriptor_ack               (w_abstract_ack_txp0 ),
                                
.ov_descriptor                  (wv_descriptor_txp0   ),
.o_descriptor_wr                (w_descriptor_wr_txp0 ),
.i_descriptor_ack               (w_descriptor_ack_txp0)
);
descriptor_delay_manage #(.delay_cycle(4'd7)) descriptor_delay_manage_p10_inst
(                              
.i_clk                          (i_clk),
.i_rst_n                        (i_rst_n),
                                
.iv_descriptor                  (wv_pkt_abstract_rxp1  ),
.i_descriptor_wr                (w_pkt_abstract_wr_rxp1),
.o_descriptor_ack               (w_abstract_ack_rxp1 ),
                                
.ov_descriptor                  (wv_descriptor_rxp1   ),
.o_descriptor_wr                (w_descriptor_wr_rxp1 ),
.i_descriptor_ack               (w_descriptor_ack_rxp1)
);
descriptor_delay_manage #(.delay_cycle(4'd7)) descriptor_delay_manage_p11_inst
(                              
.i_clk                          (i_clk),
.i_rst_n                        (i_rst_n),
                                
.iv_descriptor                  (wv_pkt_abstract_txp1  ),
.i_descriptor_wr                (w_pkt_abstract_wr_txp1),
.o_descriptor_ack               (w_abstract_ack_txp1 ),
                                
.ov_descriptor                  (wv_descriptor_txp1   ),
.o_descriptor_wr                (w_descriptor_wr_txp1 ),
.i_descriptor_ack               (w_descriptor_ack_txp1)
);
descriptor_delay_manage #(.delay_cycle(4'd7)) descriptor_delay_manage_p20_inst
(                              
.i_clk                          (i_clk),
.i_rst_n                        (i_rst_n),
                                
.iv_descriptor                  (wv_pkt_abstract_rxp2  ),
.i_descriptor_wr                (w_pkt_abstract_wr_rxp2),
.o_descriptor_ack               (w_abstract_ack_rxp2 ),
                                
.ov_descriptor                  (wv_descriptor_rxp2   ),
.o_descriptor_wr                (w_descriptor_wr_rxp2 ),
.i_descriptor_ack               (w_descriptor_ack_rxp2)
);
descriptor_delay_manage #(.delay_cycle(4'd7)) descriptor_delay_manage_p21_inst
(                              
.i_clk                          (i_clk),
.i_rst_n                        (i_rst_n),
                                
.iv_descriptor                  (wv_pkt_abstract_txp2  ),
.i_descriptor_wr                (w_pkt_abstract_wr_txp2),
.o_descriptor_ack               (w_abstract_ack_txp2 ),
                                
.ov_descriptor                  (wv_descriptor_txp2   ),
.o_descriptor_wr                (w_descriptor_wr_txp2 ),
.i_descriptor_ack               (w_descriptor_ack_txp2)
);
descriptor_delay_manage #(.delay_cycle(4'd7)) descriptor_delay_manage_p30_inst
(                              
.i_clk                          (i_clk),
.i_rst_n                        (i_rst_n),
                                
.iv_descriptor                  (wv_pkt_abstract_rxp3  ),
.i_descriptor_wr                (w_pkt_abstract_wr_rxp3),
.o_descriptor_ack               (w_abstract_ack_rxp3 ),
                                
.ov_descriptor                  (wv_descriptor_rxp3   ),
.o_descriptor_wr                (w_descriptor_wr_rxp3 ),
.i_descriptor_ack               (w_descriptor_ack_rxp3)
);
descriptor_delay_manage #(.delay_cycle(4'd7)) descriptor_delay_manage_p31_inst
(                              
.i_clk                          (i_clk),
.i_rst_n                        (i_rst_n),
                                
.iv_descriptor                  (wv_pkt_abstract_txp3  ),
.i_descriptor_wr                (w_pkt_abstract_wr_txp3),
.o_descriptor_ack               (w_abstract_ack_txp3 ),
                                
.ov_descriptor                  (wv_descriptor_txp3   ),
.o_descriptor_wr                (w_descriptor_wr_txp3 ),
.i_descriptor_ack               (w_descriptor_ack_txp3)
);

time_division time_division_multiplexing_inst(
.i_clk                          (i_clk                      ),
.i_rst_n                        (i_rst_n                    ),
                                                           
.iv_descriptor_p0               (wv_descriptor_rxp0         ), 
.i_descriptor_wr_p0             (w_descriptor_wr_rxp0       ), 
.o_descriptor_ack_p0            (w_descriptor_ack_rxp0      ), 
                                                               
.iv_descriptor_p1               (wv_descriptor_txp0         ), 
.i_descriptor_wr_p1             (w_descriptor_wr_txp0       ), 
.o_descriptor_ack_p1            (w_descriptor_ack_txp0      ), 
                                                               
.iv_descriptor_p2               (wv_descriptor_rxp1         ), 
.i_descriptor_wr_p2             (w_descriptor_wr_rxp1       ),
.o_descriptor_ack_p2            (w_descriptor_ack_rxp1      ),
                                                              
.iv_descriptor_p3               (wv_descriptor_txp1         ),
.i_descriptor_wr_p3             (w_descriptor_wr_txp1       ),
.o_descriptor_ack_p3            (w_descriptor_ack_txp1      ),
                                                              
.iv_descriptor_p4               (wv_descriptor_rxp2         ),
.i_descriptor_wr_p4             (w_descriptor_wr_rxp2       ),
.o_descriptor_ack_p4            (w_descriptor_ack_rxp2      ),  
                                                               
.iv_descriptor_p5               (wv_descriptor_txp2         ), 
.i_descriptor_wr_p5             (w_descriptor_wr_txp2       ), 
.o_descriptor_ack_p5            (w_descriptor_ack_txp2      ), 
                                                                 
.iv_descriptor_p6               (wv_descriptor_rxp3         ),
.i_descriptor_wr_p6             (w_descriptor_wr_rxp3       ),
.o_descriptor_ack_p6            (w_descriptor_ack_rxp3      ),
                                
.iv_descriptor_p7               (wv_descriptor_txp3         ),
.i_descriptor_wr_p7             (w_descriptor_wr_txp3       ),
.o_descriptor_ack_p7            (w_descriptor_ack_txp3      ),
                         
.ov_tdm_state                   (                           ),

.ov_descriptor                  ( wv_pkt_abstract           ),
.o_descriptor_wr                ( wv_pkt_abstract_wr        )
);
/*
asyncfifo_w128d64_aclr_showahead packet_abstract_cache(
  .data  (wv_pkt_abstract),
  .wrreq (wv_pkt_abstract_wr),
  .rdreq (rdreq),
  .clock (i_clk   ),
  .aclr  (!i_rst_n),
  .q     (pkt_abstract_data),   
  .usedw (),
  .full  (),
  .empty (r_empty)
);
*/
asyncfifo_w128d64_aclr_showahead packet_abstract_cache(
.srst      (!i_rst_n),                    //Reset the all signal
.din       (wv_pkt_abstract),                       //The Inport of data 
.rd_en     (rdreq),       //active-high
.clk       (i_clk),                       //ASYNC WriteClk(), SYNC use wrclk
.wr_en     (wv_pkt_abstract_wr),                     //active-high
.dout      (pkt_abstract_data),   //The output of data
.full      (),                            //Write domain full 
.empty     (r_empty),    //Write domain empty
.data_count()                            //Read-usedword
);

fit_lookup fit_lookup_inst(
.i_clk					(i_clk),
.i_rst_n				(i_rst_n),
	
.iv_fifo_rdata      	(pkt_abstract_data),
.i_fifo_empty     		(r_empty),        
.o_fifo_rd   			(rdreq),
.i_fifo_full 			(i_fifo_full    ),
.o_fitram_rd 	    	(o_fitram_rd 	),	 
.ov_fitram_raddr    	(ov_fitram_raddr), 
.iv_fitram_rdata    	(iv_fitram_rdata),
.ov_pkt_abstract    	(ov_pkt_abstract),
.ov_pkt_abstract_wr 	(o_pkt_abstract_wr),	
.iv_mirror_mode			(iv_mirror_mode)

);
endmodule